App({

  /**
   * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
   */
  onLaunch: function () {

  },

  globalData: {
    socket: null,
    socketQueue: [],
    socketPath: 'ws://localhost:8024/wx/qrsocket/',
    socketweb: null, // twt的
  },
initSocketWeb(){  //twt的
  if(wx.getStorageSync('token')){
    // 建立长连接
 this.globalData.socketweb=wx.connectSocket({
   url:`ws://192.168.60.28:8848/websocket/`+wx.getStorageSync('userInfo').userId
 })
 this.globalData.socketweb.onOpen(()=>{
  console.log("在这里面连接成功了吗")
})
this.globalData.socketweb.onMessage(res=>{
 console.log('在这里面收到服务器内容',res)
 wx.showModal({
  title: '账号已被封禁',
  content: '您的账号已被管理员封禁，请联系客服处理',
  showCancel: false,
  confirmText: '我知道了',
  success: () => {
    wx.clearStorageSync();
    wx.reLaunch({
      url: '/pages/index/index'
    });
  }
});
})
    }
},
  initSocket(path) {
    this.globalData.socket = wx.connectSocket({
      url: path
    })

    this.globalData.socket.onOpen(() => {
      console.log('连接成功')
      this.processQueue()
    })

    this.globalData.socket.onMessage(res => {
      let data=JSON.parse(res.data)
      console.log(JSON.parse(res.data))
      console.log(data.msg)
      if(data.msg=="使用成功"){
        wx.showToast({
          title: data.msg,
        })
        setTimeout(()=>{
          wx.reLaunch({
            url: `/pages/mine/payment/payment?tabid=2`
          });
        },1500)
      }
    })
  },

  sendMessage(data) {
    if (this.globalData.socket.readyState === 1) {
      this.globalData.socket.send({ data })
    } else {
      this.globalData.socketQueue.push(data)
    }
  },

  processQueue() {
    while (this.globalData.socketQueue.length) {
      this.sendMessage(this.globalData.socketQueue.shift())
    }
  }
})
